<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author Administrator
 */

namespace frontend\identity;

use frontend\bean\UserSession;
use yii\base\Component;
use frontend\exception\UserException;

class User extends Component {

    //put your code here
    const EVENT_BEFORE_LOGIN = 'beforeLogin';
    const EVENT_AFTER_LOGIN = 'afterLogin';
    const EVENT_BEFORE_LOGOUT = 'beforeLogout';
    const EVENT_AFTER_LOGOUT = 'afterLogout';

    public $uid;
    public $username;
    public $gid;
    public $vip_exp;
    private $_identity = array();

    protected function afterLogin($identity, $cookieBased, $duration) {
        $this->trigger(self::EVENT_AFTER_LOGIN, new UserEvent([
            'identity' => $identity,
            'cookieBased' => $cookieBased,
            'duration' => $duration,
        ]));
    }

    /**
     * This method is called before logging in a user.
     * The default implementation will trigger the [[EVENT_BEFORE_LOGIN]] event.
     * If you override this method, make sure you call the parent implementation
     * so that the event is triggered.
     * @param IdentityInterface $identity the user identity information
     * @param boolean $cookieBased whether the login is cookie-based
     * @param integer $duration number of seconds that the user can remain in logged-in status.
     * If 0, it means login till the user closes the browser or the session is manually destroyed.
     * @return boolean whether the user should continue to be logged in
     */
    protected function beforeLogin($identity, $cookieBased, $duration) {
        $event = new UserEvent([
            'identity' => $identity,
            'cookieBased' => $cookieBased,
            'duration' => $duration,
        ]);
        $this->trigger(self::EVENT_BEFORE_LOGIN, $event);

        return $event->isValid;
    }

    public function login($identity) {

        $this->_identity = $identity;

        $userSession = new UserSession;
        $userSession->setGid(1)
                ->setUid($identity['uid']);

        \Yii::$container->get('TokenAuth')->saveToken($userSession);
    }
    
    public function beforeLogout($identity){
         return true;
    }
    
    public function afterLogout($identity){
        return true;
    }

    /*
     * 登出
     */

    public function logout() {
        $identity = $this->getIdentity();
        if ($identity !== null && $this->beforeLogout($identity)) {
            
            $userSession =  \Yii::$container->get('TokenAuth')->getToken();
          
           \Yii::$container->get('TokenAuth')->delToken($userSession);
            $this->afterLogout($identity);
        }
    }

    public function recoverAuthStatus($id) {
        if ($id === null) {
            $identity = null;
        } else {
            /* @var $class IdentityInterface */
            // $class = $this->identityClass;
            $identity = \Yii::$container->get('frontend\dao\UserDao')->getUserByUid($id);
        }
        if (!$identity) {
            throw new UserException(UserException::E_USER_NOT_RESIGTER);
        }

        $this->setIdentity($identity);
        return $identity;
    }

    public function getIdentity($autoRenew = true) {

//        if ($this->_identity === false) {
//            if ($this->enableSession && $autoRenew) {
//                $this->_identity = null;
//                $this->renewAuthStatus();
//            } else {
//                return null;
//            }
//        }
        return $this->_identity;
    }

    public function setIdentity($identity) {
        $this->_identity = $identity;
    }

}
